package com.webamc.util;

import com.webamc.common.enums.Params;
import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * Created by Prash.
 * User: Choti
 * Date: Mar 15, 2009
 * Time: 7:44:47 PM
 * To change this template use File | Settings | File Templates.
 */
public class CommonUtil {

    private static final String[] emptyArr = new String[0];
    private static final Logger log = Logger.getLogger(CommonUtil.class);
    private static final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    static {
        sdf.setLenient(true);
    }

    public static int getInt(Map<String, String[]> m, String key, int index, int defVal) {
        if (m == null || key == null || m.get(key) == null) {
            return defVal;
        }

        String[] val = m.get(key);
        if (val.length <= index || isNullOrBlank(val[index])) {
            return defVal;
        }
        return Integer.parseInt(val[index]);
    }

    public static int getInt(Map<String, String[]> m, String key, int defVal) {
        return getInt(m, key, 0, defVal);
    }

    public static long getLong(Map<String, String[]> m, String key, int index, long defVal) {
        if (m == null || key == null || m.get(key) == null) {
            return defVal;
        }

        String[] val = m.get(key);
        if (val.length <= index || isNullOrBlank(val[index])) {
            return defVal;
        }

        return Long.parseLong(val[index]);
    }

    public static long getLong(Map<String, String[]> m, String key, long defVal) {
        return getLong(m, key, 0, defVal);
    }

    public static double getDouble(Map<String, String[]> m, String key, double defVal) {
        return getDouble(m, key, 0, defVal);
    }

    public static double getDouble(Map<String, String[]> m, String key, int index, double defVal) {
        if (m == null || key == null || m.get(key) == null) {
            return defVal;
        }

        String[] val = m.get(key);
        if (val.length <= index || isNullOrBlank(val[index])) {
            return defVal;
        }

        return Double.parseDouble(val[index]);
    }

    public static String getString(Map<String, String[]> m, String key, int index, String defVal) {
        if (m == null || key == null || m.get(key) == null) {
            return defVal;
        }

        String[] val = m.get(key);
        if (val.length <= index || isNullOrBlank(val[index]) || Params.NA.equals(val[index])) {
            return defVal;
        }

        return val[index];
    }

    public static String getString(Map<String, String[]> m, String key, String defVal) {
        return getString(m, key, 0, defVal);
    }

    public static long getTime(Map<String, String[]> m, String key, long defVal) {
        Date d = getDate(m, key, 0, defVal);
        return d != null ? d.getTime() : defVal;
    }

    public static Date getDate(Map<String, String[]> m, String key, long defVal) {
        return getDate(m, key, 0, defVal);
    }

    public static Date getDate(Map<String, String[]> m, String key, int index, long defVal) {
        if (m == null || key == null || m.get(key) == null) {
            return new Date(defVal);
        }

        String[] val = m.get(key);
        if (val.length <= index || isNullOrBlank(val[index])) {
            return new Date(defVal);
        }

        return getDate(val[index]);
    }

    public static Date getDate(String dateStr) {
        log.debug("called getDateString('" + dateStr + "')");
        synchronized (sdf) {
            try {
                return sdf.parse(dateStr);
            } catch (ParseException e) {
                log.error("failed conversion of getDateString('" + dateStr + "'), ", e);
            }

            return null;
        }
    }

    public static String getDateString(long time) {
        if (time <= 0) {  //dont show 01-JAN-1970
            return Params.BLANK;
        }

        Date d = new Date(time);
        synchronized (sdf) {
            return sdf.format(d);
        }
    }

    public static boolean isNullOrBlank(String val) {
        return (val == null || val.matches("\\s*") || val.equalsIgnoreCase(Params.NULL));
    }

    public static boolean getBool(Map<String, String[]> m, String key, boolean defVal) {
        return getBool(m, key, 0, defVal);
    }

    private static boolean getBool(Map<String, String[]> m, String key, int index, boolean defVal) {
        if (m == null || key == null || m.get(key) == null) {
            return defVal;
        }

        String[] val = m.get(key);
        if (val.length <= index || isNullOrBlank(val[index])) {
            return defVal;
        }

        return Params.TRUE.equals(val);
    }

    public static String[] trimToArray(String param, String delim) {
        if (isNullOrBlank(param)) {
            return emptyArr;
        }
        String[] retArr = param.split(delim);
        for (int i = 0; i < retArr.length; i++) {
            retArr[i] = retArr[i].trim();
        }
        return retArr;
    }

    public static int getIndex(String[] arr, String findMe) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].equals(findMe)) {
                return i;
            }
        }

        return -1;
    }

    public static String out(String s) {
        return s != null ? s : Params.BLANK;
    }

    public static String upper(String s) {
        return s != null ? s.toUpperCase() : null;
    }

    public static String lower(String s) {
        return s != null ? s.toLowerCase() : null;
    }
}